package Polyglot;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.juint.UInt16;
import util.UInt64;

public class PgShow {	
	private final static String promote_pieces=" nbrq";		
	
	public static List<PgEntry> getEntriesFromKey(File file, UInt64 key){
		PgEntry entry = new PgEntry();
		byte rawEntry[] = new byte[16];
		ArrayList<PgEntry> entries = new ArrayList<PgEntry>();
		int i;
		try {
			i = findKey(file, key, entry);
			if(i!=-1){
				if(entry.getKey().equals(key)){
					entries.add(entry);
					RandomAccessFile raf = new RandomAccessFile(file, "r");				
					while(i<=(raf.length()/16) && entry.getKey().equals(key)){
					    	raf.seek(16*i);
						    raf.read(rawEntry);
						    entry = new PgEntry(rawEntry);
						    entries.add(new PgEntry(rawEntry));
					    	i=i+16;
					}
					raf.close();
					/*for(PgEntry ent: entries){
						System.out.println(moveToString(ent.getMove()) + " " + ent.getWeight().uint16Value() );
					}*/
				}/*else{
					System.out.println("key not found");
				}*/
			}/*else{
				System.out.println("nothing there");
			}*/
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entries;
	}
	
	private static int findKey(File file, UInt64 key, PgEntry entry) throws IOException{		
		int first, last, middle;
		RandomAccessFile raf = new RandomAccessFile(file, "r");
		byte rawEntry[] = new byte[16];
		
	    PgEntry first_entry= new PgEntry();
	    PgEntry last_entry= new PgEntry();
	    PgEntry middle_entry = new PgEntry();
	    	    
	    first=-1;
	    
	    if(raf.length()<16){
	    	raf.close();
	        return -1;
	    }
	    
	    last=(int) (raf.length()/16);
	    raf.seek(raf.length()-16);
	    raf.read(rawEntry);
	    last_entry.setEntryFromByteArray(rawEntry);
	    
	    while(true){
	        if(last-first==1){
	            entry.setKey(last_entry.getKey());
	            entry.setLearn(last_entry.getLearn());
	            entry.setMove(last_entry.getMove());
	            entry.setWeight(last_entry.getWeight());
	            raf.close();
	            return last;
	        }
	        middle=(first+last)/2;
	        raf.seek(16*middle);
	        raf.read(rawEntry);
	        middle_entry.setEntryFromByteArray(rawEntry);	
	        
	        if(key.compareTo(middle_entry.key) <=0){
	            last=middle;
	            last_entry=middle_entry;
	        }else{
	            first=middle;
	            first_entry=middle_entry;
	        }
	    }
	}
	
	public static String moveToString(UInt16 move){
	    int f,fr,ff,t,tr,tf,p;
	    char res[] = new char[6];
	    f=(move.uint16Value()>>6)&077;
	    fr=(f>>3)&0x7;
	    ff=f&0x7;
	    t=move.uint16Value()&077;
	    tr=(t>>3)&0x7;
	    tf=t&0x7;
	    p=(move.uint16Value()>>12)&0x7;
	    res[0]=(char) (ff+'a');
	    res[1]=(char) (fr+'1');
	    res[2]=(char) (tf+'a');
	    res[3]=(char) (tr+'1');
	    
	    if(p>0){
	        res[4]=promote_pieces.charAt(p);
	        res[5]='\0';
	    }else{
	        res[4]='\0';
	    }
	    
	    String ret = new String(res);
	    
	    if(ret.equals("e1h1")){
	        ret="e1g1";
	    }else  if(ret.equals("e1a1")){
	    	ret="e1c1";
	    }else  if(ret.equals("e8h8")){
	    	ret="e8g8";
	    }else  if(ret.equals("e8a8")){
	    	ret="e8c8";
	    }
	    return ret;
	}
}
